// 请帖模块请帖展示路由器
//引入express模块
const express = require('express');
//引入数据库连接池模块
const pool = require('../pool.js');
//创建路由器对象
const r = express.Router();
// -------------------------------------------------------------------- 
// 1.创建请帖/视频 请帖子模块 get /show(测试成功!)http://127.0.0.1:8080/v1/inva/show/6
r.get('/show/:style_id', (req, res) => {
    //获取地址栏传参 style_id
    var _style_id = req.params.style_id;
    // console.log(_style_id);
    // 获取页码
    var _page = req.query.page;
    var min = 10 * (_page - 1) + 1;
    //根据请求的请帖种类查请帖清单
    var sql = `select inv_title,index_pic from invatation where style_id = ? limit ${min},10`;
    pool.query(sql, [_style_id], (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});
// 1.1特定种类的请帖的页码接口    http://127.0.0.1:8080/v1/inva/pages
r.get('/pages', (req, res) => {
    // 种类用查询字符串传参
    var _style_id = req.query.style_id;
    var sql = `select count(*)/10 as page_num from invatation where style_id = ${_style_id}`;
    pool.query(sql, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: "服务器端错误!" })
        } else {
            res.send(result);
            console.log(result);
        }
    });
});
// 2.获取所有请帖信息 get /all(测试成功!)http://127.0.0.1:8080/v1/inva/all
r.get('/all', (req, res) => {
    // 获取页码
    var _page = req.query.page;
    var min = 10 * (_page - 1) + 1;
    // console.log(`min=${min}`);
    var sql = `select inv_title,index_pic from invatation limit ${min},10`;
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});
// 2.2所有的请帖的页码接口    http://127.0.0.1:8080/v1/inva/all_pages
r.get('/all_pages', (req, res) => {
    var sql = `select count(*)/10 as page_num from invatation`;
    pool.query(sql, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: "服务器端错误!" })
        } else {
            res.send(result);
            console.log(result);
        }
    });
});
// 3.获取推荐的请帖信息 get /command(测试成功!)http://127.0.0.1:8080/v1/inva/recommand
r.get('/recommand', (req, res) => {
    // 获取页码
    var _page = req.query.page;
    console.log(`目前推荐请帖显得的是第${_page}页`);
    var min = 10 * (_page - 1) + 1;
    // var max = 10 * _page;
    var sql = `select inv_title,index_pic from invatation where is_recommand = '1' limit ${min},10`;
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            res.send(result);
        }
    });
});
// 3.3推荐的请帖的页码接口    http://127.0.0.1:8080/v1/inva/rec_pages
r.get('/rec_pages', (req, res) => {
    var sql = `select count(*)/10 as page_num from invatation where is_recommand = '1'`;
    pool.query(sql, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: "服务器端错误!" })
        } else {
            res.send(result);
            console.log(result);
        }
    });
});
//4.查询请帖种类 get /inv_style       http://127.0.0.1:8080/v1/inva/inv_style
r.get('/inv_style', (req, res) => {
    var sql = `select style_title from invatation_style`;
    pool.query(sql, (err, result) => {
        if (err) res.send({ code: 500, msg: "服务器端错误!" });
        if (result.length == 0) {
            res.send({ code: 400, msg: '数据库中没有相应记录' });
        } else {
            console.log(11);
            res.send(result);
        }
    });
});
// --------------------------------------------------------------------
//导出用户路由模块
module.exports = r;